<?php

require_once(WSB_XTENSION_PATH . 'gallery/models/DbTable/Table.php');

class Gallery_IndexController extends WSB_Xtension_Content
{

	private $_imgWidth;
	private $_imgHeight;
	private $_albumWidth;
	private $_albumHeight;

	public function init()
	{
		$config = $this->getConfiguration();
		$this->_imgWidth = isset($config->imgWidth) ? $config->imgWidth : 0 ;
		$this->_imgHeight = isset($config->imgHeight) ? $config->imgHeight : 100 ;
		$this->_albumWidth = isset($config->albumWidth) ? $config->albumWidth : 600 ;
		$this->_albumHeight = isset($config->albumHeight) ? $config->albumHeight : 400 ;
		parent::init();
	}

	public function indexAction()
	{
		$table = $this->getTable();
		$where = $table->getAdapter()->quoteInto('id=?', $this->_container->item_id);
		$row = $table->fetchRow($where);

		$images = array();

		if($row instanceof WSB_Db_Row) {
			$csvImages = WSB_Div::csvToArray($row->images, ',');
			$csvCaptions = WSB_Div::csvToArray($row->captions, "\n");
			foreach($csvImages AS $i=>$image) {
				if(count($images) > 2) break;
				if(is_file(WSB_PATH . $image)) {
					$imgObj = new WSB_Image(WSB_PATH . $image);
					$scaledImgObj = $imgObj->getScaledImage($this->_imgWidth, $this->_imgHeight);
					$hasContent = true;
					$images[] = array(
						'image' => $scaledImgObj->getHtml(),
						'caption' => isset($csvCaptions[$i]) ? $csvCaptions[$i] : ''
					);
				}
			}

			if(count($images) > 0) {
				$url = WSB_Xtension_Url::getInstance();

				// Album Href
				$hrefAlbum = $url->getHrefToThisPage(array('gallery' => array('action' => 'album', 'id' => $row->id, 'cid' => $this->getContainer()->id)));

				// Album Link Attribute
				$onclick = '';
				$attributes = array();
				if((bool)$row->popup) {
					$winParams = sprintf('width=%d,height=%d,status=0,menubar=0,scrollbars=1,resizable=1', $this->_albumWidth, $this->_albumHeight);
					$attributes['onclick'] = $url->getOnClick($hrefAlbum, 'Gallery-Albumg', $winParams);
					$onclick = sprintf(' onclick="%s"', $attributes['onclick']);
				}

				// Album Label
				$labelAlbum = sprintf($this->translate('start_album'), count($csvImages));

				$this->_view->hrefAlbum = $hrefAlbum;
				$this->_view->onclick = $onclick;
				$this->_view->albumLink = $url->buildHtmlLink($hrefAlbum, $labelAlbum, $attributes);
				$this->_view->class = sprintf('gallery gallery-%s gallery-layout-%s', $this->_module->id, intval($row->layout));
				$this->_view->title = $row->title;
				$this->_view->images = $images;
				$this->addContent($this->_view->render('index.phtml'));
			}
		}

	}

	public function albumAction()
	{

		$id = intval($this->getParam('id'));
		$cid = intval($this->getParam('cid'));

		// Auf einer Seite koennen mehrere Gallerien eingebunden sein.
		// Die Anzeige fuer das Album ist nur fuer das entsprechende
		// Containerelement moeglich.
		if($cid !== intval($this->getContainer()->id)) {
			$this->indexAction();
			return;
		}

		$table = $this->getTable();
		$where = $table->getAdapter()->quoteInto('id=?', $id);
		$row = $table->fetchRow($where);
		if($row instanceof WSB_Db_Row) {

			$popup = (bool)$row->popup;

			$url = WSB_Xtension_Url::getInstance();
			$playlistUrl = html_entity_decode(urldecode($url->getHrefToThisPage(array('gallery' => array('action' => 'playlist', 'id' => $id)))));
			/*
			See http://www.jeroenwijering.com/?item=Supported_Flashvars
			? ? %3F
			= ? %3D
			& ? %26
			*/
			$from = array('?', '=', '&');
			$to = array('%3F', '%3D', '%26');

			$this->_view->rotatorId = sprintf('rotator-%d', $id);
			$this->_view->title = $row->title;
			$this->_view->popup = $popup;
			$this->_view->playlistUrl = str_replace($from, $to, $playlistUrl);
			$this->_view->width = $this->_albumWidth;
			$this->_view->height = $this->_albumHeight;
			$this->_view->relPathToResFolder = WSB_URL . 'WSB/xtensions/gallery/res/';

			$content = $this->_view->render('album.phtml');
			if($popup) {
				echo $content;
				exit;
			} else {
				$this->addContent($content);
			}
		}
	}

	public function playlistAction()
	{

		$table = $this->getTable();
		$where = $table->getAdapter()->quoteInto('id=?', $this->getParam('id'));
		$row = $table->fetchRow($where);

		$playlist = array();

		if($row instanceof WSB_Db_Row) {
			$images = WSB_Div::csvToArray($row->images, ',');
			$captions = WSB_Div::csvToArray($row->captions, "\n");
			foreach($images AS $i=>$image) {
				if(is_file(WSB_PATH . $image)) {
					$playlist[] = array(
						'title' => isset($captions[$i]) ? $captions[$i] : '&nbsp;',
						'location' => WSB_URL . $image
					);
				}
			}
		}

		header('Content-type: text/xml');
		header("Pragma:no-cache");
		header("Cache-Control:private,no-store,no-cache,must-revalidate");

		$this->_view->xmlprologue = '<?xml version="1.0" encoding="utf-8"?>';
		$this->_view->playlist = $playlist;

		echo $this->_view->render('playlist.phtml');
		exit;

	}

	public function getTable()
	{
		return new Gallery_Model_DbTable_Table();
	}

}
